package sw6.pc.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import java.awt.Font;
import java.net.UnknownHostException;

import sw6.pc.network.*;
import javax.swing.SwingConstants;

public class ConnectingForm {

	private JFrame frame;

	/**
	 * Launch the application.
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		
		//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ConnectingForm window = new ConnectingForm();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ConnectingForm() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblWaitingForDevice = new JLabel("Waiting for device...");
		lblWaitingForDevice.setHorizontalAlignment(SwingConstants.CENTER);
		lblWaitingForDevice.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblWaitingForDevice.setBounds(10, 50, 414, 45);
		frame.getContentPane().add(lblWaitingForDevice);
		
		JLabel lblComputerIp = new JLabel("Computer IP: ");
		lblComputerIp.setHorizontalAlignment(SwingConstants.CENTER);
		lblComputerIp.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblComputerIp.setBounds(10, 105, 414, 45);
		frame.getContentPane().add(lblComputerIp);
		
		try {
			lblComputerIp.setText("Computer IP: " + new Server().getLocalIpAddress());
			
			JButton btnNewButton = new JButton("New button");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					onButtonClick();
				}
			});
			btnNewButton.setBounds(162, 176, 89, 23);
			frame.getContentPane().add(btnNewButton);
		} catch (UnknownHostException e) {
			lblComputerIp.setText("Failed to get computer IP!");
		}
	}
	
	private void onButtonClick() {
		frame.setVisible(false);
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdministrationForm frame = new AdministrationForm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
